package com.clouds.wms.dao;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;
import com.clouds.wms.bean.Invmc;
import com.clouds.wms.exception.DatabaseException;
import com.clouds.wms.util.Function;
import com.clouds.wms.util.PagesBean;
import com.clouds.wms.util.SysParams;

@Repository("invmcDao")
public class InvmcDaoImpl implements InvmcDao {
	private Log log = LogFactory.getLog(getClass());
	@Resource(name="namedParameterJdbcTemplate")
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
	@Override
	public PagesBean getInvmcList(String maaa, String maab, String maac,String startMaad, String endMaad, int offset, int pageSize)	throws DatabaseException {
		PagesBean pb = new PagesBean();
		List<Invmc> list = new ArrayList<Invmc>();
		boolean hasWhere = false;
		StringBuffer sql = new StringBuffer("SELECT * FROM invmc ");
		if(maaa != null && !"".equals(maaa)) {
			hasWhere = Function.appendWhereIfNeed(sql, hasWhere);
			sql.append( " maaa = '" + Function.formatSQLString(maaa) + "'");
		}
		if(maab != null && !"".equals(maab)) {
			hasWhere = Function.appendWhereIfNeed(sql, hasWhere);
			sql.append( " maab = '" + Function.formatSQLString(maab) + "'");
		}
		if(maac != null && !"".equals(maac)) {
			hasWhere = Function.appendWhereIfNeed(sql, hasWhere);
			sql.append( " maac = '" + Function.formatSQLString(maac) + "'");
		}else{
			hasWhere = Function.appendWhereIfNeed(sql, hasWhere);
			sql.append( " maac in ('" + SysParams.AUDIT_YARDS_NOT_AUDIT + "','S') ");
		}
		if(startMaad != null && !"".equals(startMaad)) {
			sql.append( " maad >= '" + Function.formatSQLString(startMaad) + "' ");
		}
		if(endMaad != null && !"".equals(endMaad)) {
			sql.append(" maad <= '" + Function.formatSQLString(endMaad) + "' ");
		}
		sql.append(" ORDER BY maab DESC");
		try {
			int totalCount = namedParameterJdbcTemplate.getJdbcOperations().queryForInt(Function.toCountSql(sql.toString()));
			if(totalCount!=0){
				list = namedParameterJdbcTemplate.getJdbcOperations().query(Function.mySqlQuery(sql.toString(),offset,pageSize,totalCount),new BeanPropertyRowMapper<Invmc>(Invmc.class));
			}
			pb.setCount(totalCount);
			pb.setData(list);
			return pb;
		} catch (EmptyResultDataAccessException e) {
			log.error("exception :", e);
			throw new DatabaseException(e.getMessage());
		}
	}
	@Override
	public Invmc getInvmc(String maaa,String maab) throws DatabaseException {
		String sql = "SELECT * FROM invmc WHERE maaa=? and maab = ? ";
		try {
			return namedParameterJdbcTemplate.getJdbcOperations().queryForObject(sql, new Object[]{maaa,maab}, new BeanPropertyRowMapper<Invmc>(Invmc.class));
		} catch (EmptyResultDataAccessException e) {
			log.info("没有数据");
			return null;
		} catch (Exception e) {
			log.error("exception :", e);
			throw new DatabaseException(e.getMessage());
		}
	}
	@Override
	public int saveInvmc(Invmc invmc) throws DatabaseException {
		String sql = "INSERT INTO invmc (maaa,maab,maad) VALUES (:maaa,:maab,:maad)";
		try {
			SqlParameterSource param = new BeanPropertySqlParameterSource(invmc);
			return namedParameterJdbcTemplate.update(sql, param);
		} catch (Exception e) {
			log.error("exception :", e);
			throw new DatabaseException(e.getMessage());
		}
	}
	@Override
	public void delInvmcByAaAb(String maaa, String maab)throws DatabaseException {
		String sql1 = "delete from invmc where maaa = ? and maab = ?";
		namedParameterJdbcTemplate.getJdbcOperations().update(sql1, new Object[]{maaa,maab});
	}

}
